package com.house365.core.util.store;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.entity.mime.content.StringBody;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.house365.core.constant.CorePreferences;
import com.house365.core.json.JSONArray;
import com.house365.core.json.JSONException;
import com.house365.core.json.JSONObject;
import com.house365.core.reflect.ReflectException;
import com.house365.core.reflect.ReflectUtil;

/**
 * @author py
 * @date 2012-6-13
 * @comment
 */
public class SharedPreferencesUtil {
	private final SharedPreferences mPrefs;

	public SharedPreferencesUtil(Context context) {
		mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
	}
	public SharedPreferencesUtil(SharedPreferences mPrefs) {
		this.mPrefs = mPrefs;
	}
	public SharedPreferences getSharedPreferences() {
		return mPrefs;
	}

	/**
	 * ���
	 * 
	 * @param type
	 */
	public void clean(String key) {
		mPrefs.edit().remove(key).commit();
	}
	/**
	 * 清除相关key的值
	 * */
	public void cleanContain(String key){
		try{
		Map<String, String> allPrefs = (Map<String, String>) mPrefs.getAll();
		for(Map.Entry<String, String> entry: allPrefs.entrySet()) {
			CorePreferences.DEBUG("share:"+entry.getKey());
			if(entry.getKey().contains(key)){
				mPrefs.edit().remove(entry.getKey()).commit();
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * ��һ��list�洢��ʽ�����һ��Ԫ��
	 * 
	 * @param key
	 * @param t
	 */
	public <T> void addListItem(String key, T t) {
		// �����Ƚ��洢����Ϣȡ����
		JSONArray array = getList(key);
		array.put(new JSONObject(t));
		mPrefs.edit().putString(key, array.toString()).commit();

	}

	/**
	 * ���һ��list���
	 * 
	 * @param key
	 * @param list
	 */
	public void putList(String key, List list) {
		mPrefs.edit().putString(key, new JSONArray(list).toString()).commit();
	} 

	public String[] getStringArray(String key) {
		String str = mPrefs.getString(key, null);
		if (str != null) {
			String[] temparr = str.split(",");
			return temparr;
		} else {
			return null;
		}
	}
	
	public boolean isHasString(String key,String val){
		String[] temparray = getStringArray(key);
		if(temparray!=null){
			for(int i=0;i<temparray.length;i++){
				if(val.equals(temparray[i])){
					return true;
				}
			}
		}
		return false;
	}
	//�����ַ�
	public boolean isIndexOfString(String key,String val){
		String splitag=",";
		String str = mPrefs.getString(key, null);
		if(str!=null){
			str =splitag+str;
			if(str.indexOf(splitag+val)>-1){
				return true;
			}
		}
		return false;
	}
	
	public void removeArrayString(String key,String val){
		String splitag=",";
		String str = mPrefs.getString(key, null);
		String tempStr="";
		if(str!=null && !"".equals(str)){
			tempStr =splitag+str;
			str = tempStr.replace(splitag+val, "");
			if(str!=null && !"".equals(str)){
				tempStr = str.substring(1);
			}else{
				tempStr = str;
			}
		}
		CorePreferences.DEBUG("removeArrayString:"+tempStr);
		mPrefs.edit().putString(key, tempStr).commit();
	}
	
	public void addArrayString(String key,String val){
		String splitag=",";
		String str = mPrefs.getString(key, null);
		String tempStr="";
		if(str!=null && !"".equals(str)){
			tempStr =str+splitag+val;
		}else{
			tempStr =val;
		}
		CorePreferences.DEBUG("addArrayString:"+tempStr);
		mPrefs.edit().putString(key, tempStr).commit();
	}
	 

	/**
	 * ��ȡһ��list������jsonarray�ķ�ʽ����
	 * 
	 * @param key
	 * @return
	 */
	public JSONArray getList(String key) {
		String oldInfo = mPrefs.getString(key, null);
		JSONArray oldInfoArray;
		if (oldInfo == null) {
			oldInfoArray = new JSONArray();
		} else {
			try {
				oldInfoArray = new JSONArray(oldInfo);
			} catch (JSONException e) {
				e.printStackTrace();
				oldInfoArray = new JSONArray();
			}
		}
		return oldInfoArray;
	}

	/**
	 * ��ȡ��ʷ��¼������ת����Ķ���
	 * 
	 * @param type
	 * @return
	 * @throws ReflectException
	 * @throws JSONException
	 */
	public <T extends SharedPreferencesDTO> List<T> getListWithCast(T t,
			String key) throws ReflectException, JSONException {
		JSONArray jsonArray = getList(key);
		List<T> list = new ArrayList<T>();
		T temp;
		for (int i = 0; i < jsonArray.length(); i++) {
			temp = (T) ReflectUtil.copy(t.getClass(),
					jsonArray.getJSONObject(i));
			list.add(temp);
		}
		return list;
	}

	
	
	/**
	 * ɾ��ĳһ���ղ�
	 * 
	 * @param houseInfo
	 */
	public <T extends SharedPreferencesDTO> void removeListItem(String key, T t) {
		JSONArray infoArray = getList(key);
		int flag = -1;
		for (int i = 0; i < infoArray.length(); i++) {
			try {
				T temp = (T) ReflectUtil.copy(t.getClass(),
						infoArray.getJSONObject(i));
				if (t.isSame(temp)) {
					flag = i;
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (flag != -1) {
			infoArray.remove(flag);
			mPrefs.edit().putString(key, infoArray.toString()).commit();
		}

	}

	/**
	 * �Ƿ��Ѿ�������ʷ��¼
	 * 
	 * @param type
	 * @param houseInfo
	 * @return
	 */
	public <T extends SharedPreferencesDTO> boolean hasListItem(String key, T t) {
		JSONArray infoArray = getList(key);
		boolean flag = false;
		for (int i = 0; i < infoArray.length(); i++) {
			try {
				T temp = (T) ReflectUtil.copy(t.getClass(),
						infoArray.getJSONObject(i));
				if (t.isSame(temp)) {
					flag = true;
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	public void putBoolean(String key, boolean value) {
		mPrefs.edit().putBoolean(key, value).commit();
	}

	public void putInt(String key, int value) {
		mPrefs.edit().putInt(key, value).commit();
	}

	public void putString(String key, String value) {
		mPrefs.edit().putString(key, value).commit();
	}

	public void putFloat(String key, float value) {
		mPrefs.edit().putFloat(key, value).commit();
	}

	public void putLong(String key, long value) {
		mPrefs.edit().putLong(key, value).commit();
	}

	public void putObject(String key, Object value) {
		mPrefs.edit().putString(key, new JSONObject(value).toString()).commit();
	}

	public boolean getBoolean(String key, boolean defValue) {
		return mPrefs.getBoolean(key, defValue);
	}

	public int getInt(String key, int defValue) {
		return mPrefs.getInt(key, defValue);
	}

	public String getString(String key, String defValue) {
		return mPrefs.getString(key, defValue);
	}

	public float getFloat(String key, float defValue) {
		return mPrefs.getFloat(key, defValue);
	}

	public long getLong(String key, long defValue) {
		return mPrefs.getLong(key, defValue);
	}

	public Object getObject(String key, Class clazz)
			throws SharedPreferenceException {
		try {
			return ReflectUtil.copy(clazz,
					new JSONObject(mPrefs.getString(key, "")));
		} catch (Exception e) {
			throw new SharedPreferenceException("get object occurs exception",
					e);
		}
	}

}
